/*     */ package com.zimbra.cs.mailbox;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.redolog.op.RedoableOp;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.session.Session;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class OperationContext
/*     */ {
/*     */   public static final boolean CHECK_CREATED = false;
/*     */   public static final boolean CHECK_MODIFIED = true;
/*     */   private Account authuser;
/*     */   private boolean isAdmin;
/*     */   private Session session;
/*     */   private RedoableOp player;
/*     */   private String requestIP;
/*     */   private String userAgent;
/*     */   private AuthToken authToken;
/*     */   private Map<String, OperationContextData> contextData;
/*  46 */   boolean changetype = false;
/*  47 */   int change = -1;
/*     */   
/*     */   public OperationContext(RedoableOp redoPlayer) {
/*  50 */     this.player = redoPlayer;
/*     */   }
/*     */   
/*     */   public OperationContext(Account acct) {
/*  54 */     this(acct, false);
/*     */   }
/*     */   
/*     */   public OperationContext(Mailbox mbox) throws ServiceException {
/*  58 */     this(mbox.getAccount());
/*     */   }
/*     */   
/*     */   public OperationContext(Account acct, boolean admin) {
/*  62 */     this.authuser = acct;this.isAdmin = admin;
/*     */   }
/*     */   
/*     */   public OperationContext(String accountId) throws ServiceException {
/*  66 */     this.authuser = Provisioning.getInstance().get(Key.AccountBy.id, accountId);
/*  67 */     if (this.authuser == null)
/*  68 */       throw AccountServiceException.NO_SUCH_ACCOUNT(accountId);
/*     */   }
/*     */   
/*     */   public OperationContext(AuthToken auth) throws ServiceException {
/*  72 */     this.authToken = auth;
/*  73 */     String accountId = auth.getAccountId();
/*  74 */     this.isAdmin = AuthToken.isAnyAdmin(auth);
/*  75 */     this.authuser = Provisioning.getInstance().get(Key.AccountBy.id, accountId, this.authToken);
/*  76 */     if ((this.authuser == null) || (!auth.isZimbraUser())) {
/*  77 */       if ((auth.getDigest() != null) || (auth.getAccessKey() != null)) {
/*  78 */         this.authuser = new GuestAccount(auth);
/*     */       } else {
/*  80 */         this.authuser = GuestAccount.ANONYMOUS_ACCT;
/*     */       }
/*     */     }
/*  83 */     if (this.authuser == null)
/*  84 */       throw AccountServiceException.NO_SUCH_ACCOUNT(accountId);
/*     */   }
/*     */   
/*     */   public OperationContext(OperationContext octxt) {
/*  88 */     this.player = octxt.player;this.session = octxt.session;
/*  89 */     this.authuser = octxt.authuser;this.isAdmin = octxt.isAdmin;
/*  90 */     this.changetype = octxt.changetype;this.change = octxt.change;
/*  91 */     this.authToken = octxt.authToken;
/*     */   }
/*     */   
/*     */   public OperationContext setChangeConstraint(boolean checkModified, int changeId) {
/*  95 */     this.changetype = checkModified;this.change = changeId;return this;
/*     */   }
/*     */   
/*     */   public OperationContext unsetChangeConstraint() {
/*  99 */     this.changetype = false;this.change = -1;return this;
/*     */   }
/*     */   
/*     */   public OperationContext setSession(Session s) {
/* 103 */     this.session = s;return this;
/*     */   }
/*     */   
/*     */   Session getSession() {
/* 107 */     return this.session;
/*     */   }
/*     */   
/*     */   public RedoableOp getPlayer() {
/* 111 */     return this.player;
/*     */   }
/*     */   
/*     */   public long getTimestamp() {
/* 115 */     return this.player == null ? System.currentTimeMillis() : this.player.getTimestamp();
/*     */   }
/*     */   
/*     */   int getChangeId() {
/* 119 */     return this.player == null ? -1 : this.player.getChangeId();
/*     */   }
/*     */   
/*     */   public boolean isRedo() {
/* 123 */     return this.player != null;
/*     */   }
/*     */   
/*     */   public boolean needRedo() {
/* 127 */     return (this.player == null) || (!this.player.getUnloggedReplay());
/*     */   }
/*     */   
/*     */   public Account getAuthenticatedUser() {
/* 131 */     return this.authuser;
/*     */   }
/*     */   
/*     */   public AuthToken getAuthToken() throws ServiceException {
/* 135 */     return getAuthToken(true);
/*     */   }
/*     */   
/*     */   public AuthToken getAuthToken(boolean constructIfNotPresent) throws ServiceException {
/* 139 */     if (this.authToken != null)
/* 140 */       return this.authToken;
/* 141 */     if ((constructIfNotPresent) && 
/* 142 */       (getAuthenticatedUser() != null)) {
/* 143 */       return AuthProvider.getAuthToken(getAuthenticatedUser(), isUsingAdminPrivileges());
/*     */     }
/*     */     
/* 146 */     return null;
/*     */   }
/*     */   
/*     */   public boolean isUsingAdminPrivileges() {
/* 150 */     return this.isAdmin;
/*     */   }
/*     */   
/*     */   public boolean isDelegatedRequest(Mailbox mbox) {
/* 154 */     return (this.authuser != null) && (!this.authuser.getId().equalsIgnoreCase(mbox.getAccountId()));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isOnBehalfOfRequest(Mailbox mbox)
/*     */   {
/* 161 */     if (!isDelegatedRequest(mbox)) {
/* 162 */       return false;
/*     */     }
/* 164 */     return (this.authuser != null) && (!AccountUtil.isZDesktopLocalAccount(this.authuser.getId()));
/*     */   }
/*     */   
/*     */   public OperationContext setRequestIP(String addr) {
/* 168 */     this.requestIP = addr;return this;
/*     */   }
/*     */   
/*     */   public String getRequestIP() {
/* 172 */     return this.requestIP;
/*     */   }
/*     */   
/*     */   public OperationContext setUserAgent(String ua) {
/* 176 */     this.userAgent = ua;return this;
/*     */   }
/*     */   
/*     */   public String getUserAgent() {
/* 180 */     return this.userAgent;
/*     */   }
/*     */   
/*     */   public void setCtxtData(String key, OperationContextData data) {
/* 184 */     if (this.contextData == null) {
/* 185 */       this.contextData = new HashMap();
/*     */     }
/* 187 */     this.contextData.put(key, data);
/*     */   }
/*     */   
/*     */   public OperationContextData getCtxtData(String key) {
/* 191 */     if (this.contextData == null) {
/* 192 */       return null;
/*     */     }
/* 194 */     return (OperationContextData)this.contextData.get(key);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailbox/OperationContext.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */